{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-04-21 10:48:03 [INFO]\tStarting to read file list from dataset...\n",
      "2025-04-21 10:48:03 [INFO]\t1260 samples in file ./train.txt\n",
      "2025-04-21 10:48:03 [INFO]\tStarting to read file list from dataset...\n",
      "2025-04-21 10:48:03 [INFO]\t317 samples in file ./val.txt\n"
     ]
    }
   ],
   "source": [
    "import paddlex as pdx\n",
    "from paddlex import transforms\n",
    "train_transforms=transforms.Compose([transforms.RandomCrop(crop_size=224),transforms.RandomHorizontalFlip(),\n",
    "transforms.RandomDistort(brightness_range=0.9,brightness_prob=0.5,contrast_range=0.9,contrast_prob=0.5,saturation_range=0.9,saturation_prob=0.5,hue_range=18,hue_prob=0.5),\n",
    "                                    transforms.Normalize()])\n",
    "val_transforms=transforms.Compose([transforms.ResizeByShort(short_size=256),\n",
    "                                  transforms.CenterCrop(crop_size=224),\n",
    "                                  transforms.Normalize()])\n",
    "train_dataset=pdx.datasets.ImageNet(\n",
    "    data_dir='garbage',\n",
    "    file_list='./train.txt',\n",
    "    label_list='./labels.txt',\n",
    "    transforms=train_transforms,\n",
    "    shuffle=True\n",
    ")\n",
    "val_dataset=pdx.datasets.ImageNet(\n",
    "    data_dir='garbage',\n",
    "    file_list='./val.txt',\n",
    "    label_list='./labels.txt',\n",
    "    transforms=val_transforms\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddlex as pdx\n",
    "num_classes=len(train_dataset.labels)\n",
    "model=pdx.cls.ResNet50_vd_ssld(num_classes=num_classes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2025-04-21 10:48:08 [INFO]\tLoading pretrained model from output/ResNet50_vd_ssld\\pretrain\\ResNet50_vd_ssld_pretrained.pdparams\n",
      "2025-04-21 10:48:08 [WARNING]\t[SKIP] Shape of pretrained params fc.weight doesn't match.(Pretrained: (2048, 1000), Actual: [2048, 3])\n",
      "2025-04-21 10:48:08 [WARNING]\t[SKIP] Shape of pretrained params fc.bias doesn't match.(Pretrained: (1000,), Actual: [3])\n",
      "2025-04-21 10:48:09 [INFO]\tThere are 275/277 variables loaded into ResNet50_vd_ssld.\n",
      "2025-04-21 10:49:45 [INFO]\t[TRAIN] Epoch=1/5, Step=10/78, loss=0.943266, acc1=0.687500, acc3=1.000000, lr=0.002000, time_each_step=9.61s, eta=1:4:4\n",
      "2025-04-21 10:51:29 [INFO]\t[TRAIN] Epoch=1/5, Step=20/78, loss=0.790672, acc1=0.687500, acc3=1.000000, lr=0.002000, time_each_step=10.44s, eta=1:7:51\n",
      "2025-04-21 10:53:31 [INFO]\t[TRAIN] Epoch=1/5, Step=30/78, loss=0.702863, acc1=0.687500, acc3=1.000000, lr=0.002000, time_each_step=12.22s, eta=1:17:23\n",
      "2025-04-21 10:55:31 [INFO]\t[TRAIN] Epoch=1/5, Step=40/78, loss=0.554223, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=11.94s, eta=1:13:38\n",
      "2025-04-21 10:57:31 [INFO]\t[TRAIN] Epoch=1/5, Step=50/78, loss=0.621093, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=12.04s, eta=1:12:16\n",
      "2025-04-21 10:59:29 [INFO]\t[TRAIN] Epoch=1/5, Step=60/78, loss=0.738708, acc1=0.687500, acc3=1.000000, lr=0.002000, time_each_step=11.76s, eta=1:8:36\n",
      "2025-04-21 11:01:31 [INFO]\t[TRAIN] Epoch=1/5, Step=70/78, loss=0.701095, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=12.18s, eta=1:9:2\n",
      "2025-04-21 11:03:11 [INFO]\t[TRAIN] Epoch 1 finished, loss=0.6548926, acc1=0.7147436, acc3=1.0 .\n",
      "2025-04-21 11:03:11 [INFO]\tStart to evaluate(total_samples=317, total_steps=20)...\n",
      "2025-04-21 11:04:27 [INFO]\t[EVAL] Finished, Epoch=1, acc1=0.878125, acc3=1.000000 .\n",
      "2025-04-21 11:04:27 [INFO]\tModel saved in output/ResNet50_vd_ssld\\best_model.\n",
      "2025-04-21 11:04:27 [INFO]\tCurrent evaluated best model on eval_dataset is epoch_1, acc1=0.878125011920929\n",
      "2025-04-21 11:04:28 [INFO]\tModel saved in output/ResNet50_vd_ssld\\epoch_1.\n",
      "2025-04-21 11:04:54 [INFO]\t[TRAIN] Epoch=2/5, Step=2/78, loss=0.431174, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=12.57s, eta=1:8:46\n",
      "2025-04-21 11:06:54 [INFO]\t[TRAIN] Epoch=2/5, Step=12/78, loss=0.515431, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=12.02s, eta=1:3:57\n",
      "2025-04-21 11:08:54 [INFO]\t[TRAIN] Epoch=2/5, Step=22/78, loss=0.402745, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=11.98s, eta=1:1:45\n",
      "2025-04-21 11:11:00 [INFO]\t[TRAIN] Epoch=2/5, Step=32/78, loss=0.157746, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=12.6s, eta=1:2:39\n",
      "2025-04-21 11:12:51 [INFO]\t[TRAIN] Epoch=2/5, Step=42/78, loss=0.453696, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=11.16s, eta=0:54:3\n",
      "2025-04-21 11:14:58 [INFO]\t[TRAIN] Epoch=2/5, Step=52/78, loss=0.178315, acc1=1.000000, acc3=1.000000, lr=0.002000, time_each_step=12.63s, eta=0:58:34\n",
      "2025-04-21 11:16:52 [INFO]\t[TRAIN] Epoch=2/5, Step=62/78, loss=0.107170, acc1=1.000000, acc3=1.000000, lr=0.002000, time_each_step=11.44s, eta=0:51:30\n",
      "2025-04-21 11:18:55 [INFO]\t[TRAIN] Epoch=2/5, Step=72/78, loss=0.125079, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=12.31s, eta=0:53:5\n",
      "2025-04-21 11:20:10 [INFO]\t[TRAIN] Epoch 2 finished, loss=0.36838397, acc1=0.8469551, acc3=1.0 .\n",
      "2025-04-21 11:20:10 [INFO]\tStart to evaluate(total_samples=317, total_steps=20)...\n",
      "2025-04-21 11:21:16 [INFO]\t[EVAL] Finished, Epoch=2, acc1=0.950000, acc3=1.000000 .\n",
      "2025-04-21 11:21:16 [INFO]\tModel saved in output/ResNet50_vd_ssld\\best_model.\n",
      "2025-04-21 11:21:16 [INFO]\tCurrent evaluated best model on eval_dataset is epoch_2, acc1=0.949999988079071\n",
      "2025-04-21 11:21:16 [INFO]\tModel saved in output/ResNet50_vd_ssld\\epoch_2.\n",
      "2025-04-21 11:22:03 [INFO]\t[TRAIN] Epoch=3/5, Step=4/78, loss=0.399992, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=12.19s, eta=0:48:55\n",
      "2025-04-21 11:24:11 [INFO]\t[TRAIN] Epoch=3/5, Step=14/78, loss=0.116231, acc1=1.000000, acc3=1.000000, lr=0.002000, time_each_step=12.76s, eta=0:48:59\n",
      "2025-04-21 11:26:46 [INFO]\t[TRAIN] Epoch=3/5, Step=24/78, loss=0.487859, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=15.54s, eta=0:56:36\n",
      "2025-04-21 11:29:38 [INFO]\t[TRAIN] Epoch=3/5, Step=34/78, loss=0.435130, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=17.2s, eta=0:59:32\n",
      "2025-04-21 11:32:23 [INFO]\t[TRAIN] Epoch=3/5, Step=44/78, loss=0.278046, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=16.5s, eta=0:54:27\n",
      "2025-04-21 11:34:35 [INFO]\t[TRAIN] Epoch=3/5, Step=54/78, loss=0.126823, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=13.17s, eta=0:41:43\n",
      "2025-04-21 11:36:31 [INFO]\t[TRAIN] Epoch=3/5, Step=64/78, loss=0.302869, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=11.59s, eta=0:35:2\n",
      "2025-04-21 11:38:28 [INFO]\t[TRAIN] Epoch=3/5, Step=74/78, loss=0.432632, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=11.64s, eta=0:33:14\n",
      "2025-04-21 11:39:13 [INFO]\t[TRAIN] Epoch 3 finished, loss=0.3156742, acc1=0.8717949, acc3=1.0 .\n",
      "2025-04-21 11:39:13 [INFO]\tStart to evaluate(total_samples=317, total_steps=20)...\n",
      "2025-04-21 11:40:25 [INFO]\t[EVAL] Finished, Epoch=3, acc1=0.956250, acc3=1.000000 .\n",
      "2025-04-21 11:40:26 [INFO]\tModel saved in output/ResNet50_vd_ssld\\best_model.\n",
      "2025-04-21 11:40:26 [INFO]\tCurrent evaluated best model on eval_dataset is epoch_3, acc1=0.956250011920929\n",
      "2025-04-21 11:40:26 [INFO]\tModel saved in output/ResNet50_vd_ssld\\epoch_3.\n",
      "2025-04-21 11:41:39 [INFO]\t[TRAIN] Epoch=4/5, Step=6/78, loss=0.384329, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=11.78s, eta=0:30:39\n",
      "2025-04-21 11:43:27 [INFO]\t[TRAIN] Epoch=4/5, Step=16/78, loss=0.262037, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=10.87s, eta=0:26:34\n",
      "2025-04-21 11:45:41 [INFO]\t[TRAIN] Epoch=4/5, Step=26/78, loss=0.221866, acc1=0.875000, acc3=1.000000, lr=0.002000, time_each_step=13.41s, eta=0:30:15\n",
      "2025-04-21 11:47:40 [INFO]\t[TRAIN] Epoch=4/5, Step=36/78, loss=0.431139, acc1=0.750000, acc3=1.000000, lr=0.002000, time_each_step=11.84s, eta=0:24:53\n",
      "2025-04-21 11:49:35 [INFO]\t[TRAIN] Epoch=4/5, Step=46/78, loss=0.053868, acc1=1.000000, acc3=1.000000, lr=0.002000, time_each_step=11.58s, eta=0:22:26\n",
      "2025-04-21 11:51:35 [INFO]\t[TRAIN] Epoch=4/5, Step=56/78, loss=0.348455, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=11.93s, eta=0:21:5\n",
      "2025-04-21 11:53:36 [INFO]\t[TRAIN] Epoch=4/5, Step=66/78, loss=0.525194, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=12.1s, eta=0:19:21\n",
      "2025-04-21 11:55:35 [INFO]\t[TRAIN] Epoch=4/5, Step=76/78, loss=0.319509, acc1=0.812500, acc3=1.000000, lr=0.002000, time_each_step=11.96s, eta=0:17:9\n",
      "2025-04-21 11:55:57 [INFO]\t[TRAIN] Epoch 4 finished, loss=0.25848597, acc1=0.9014423, acc3=1.0 .\n",
      "2025-04-21 11:55:57 [INFO]\tStart to evaluate(total_samples=317, total_steps=20)...\n",
      "2025-04-21 11:57:01 [INFO]\t[EVAL] Finished, Epoch=4, acc1=0.984375, acc3=1.000000 .\n",
      "2025-04-21 11:57:02 [INFO]\tModel saved in output/ResNet50_vd_ssld\\best_model.\n",
      "2025-04-21 11:57:02 [INFO]\tCurrent evaluated best model on eval_dataset is epoch_4, acc1=0.984375\n",
      "2025-04-21 11:57:02 [INFO]\tModel saved in output/ResNet50_vd_ssld\\epoch_4.\n",
      "2025-04-21 11:58:34 [INFO]\t[TRAIN] Epoch=5/5, Step=8/78, loss=0.396959, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=11.34s, eta=0:13:13\n",
      "2025-04-21 12:00:13 [INFO]\t[TRAIN] Epoch=5/5, Step=18/78, loss=0.143570, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=9.95s, eta=0:9:57\n",
      "2025-04-21 12:01:59 [INFO]\t[TRAIN] Epoch=5/5, Step=28/78, loss=0.180478, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=10.6s, eta=0:8:50\n",
      "2025-04-21 12:03:57 [INFO]\t[TRAIN] Epoch=5/5, Step=38/78, loss=0.203950, acc1=0.937500, acc3=1.000000, lr=0.002000, time_each_step=11.73s, eta=0:7:49\n"
     ]
    }
   ],
   "source": [
    "model.train(num_epochs=5,\n",
    "train_dataset=train_dataset,\n",
    "train_batch_size=16,\n",
    "eval_dataset=val_dataset,\n",
    "lr_decay_epochs=[80,100,150],\n",
    "save_interval_epochs=1,\n",
    "learning_rate=0.002,\n",
    "save_dir='output/ResNet50_vd_ssld',\n",
    "use_vdl=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = pdx.load_model('output/ResNet50_vd_ssld/best_model')\n",
    "image_name ='./data/garbage/paper/paper10.jpg'\n",
    "result = model.predict(image_name)\n",
    "print('Predict Result:',result)\n",
    "number = result[0][category] number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import paddlex\n",
    "\n",
    "img_file='./data/garbage/paper/paper10.jpg'\n",
    "model = pax load_model('output/ResNet50_vd_ssld/best_model')\n",
    "\n",
    "paddlex.interpret.lime(img_file,\n",
    "                       model,\n",
    "                       num_samples=3000，\n",
    "                       batch_size=50，\n",
    "                       save_dir='/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
